<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/19 0019
 * Time: 上午 10:09
 */
/*
 * 获取header信息
 *
 * */
function getAllHeader(){
    foreach ($_SERVER as $name => $value)
    {
        if (substr($name, 0, 5) == 'HTTP_')
        {
            $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
        }
    }
    return $headers;
}

//验证TOKEN

function check_token($token){

    $arr['token'] = array('eq',$token);
    $value = M('token_value')->where($arr)->find();
    if(!$value){
        $data['code'] = 500;
        $data['msg'] = 'token验证失败';
        exit(json_encode($data));
    }
    if($value['invalid_time'] < time()){
        $data['code'] = 501;
        $data['msg'] = 'token已过期';
        exit(json_encode($data));
    }
    return true;
}


function headerInfo(){
    $header = apache_request_headers();
    $authorization = explode('_',$header['Authorization']);
    return $authorization;
}

//缩略图
function get_icon_img($icon_url,$icon){
    if($icon>0){
        return picture($icon);
    }else{
        if($icon_url!=''){
            return $icon_url;
        }else{
            return picture($icon);
        }
    }
}

//上传图片并返回图片id 字符串 支持多图
function add_imge(){
    $config = C('PICTURE_UPLOAD');
    $upload = new \Think\Upload($config);
    $info = $upload->upload();

    if($info) {
        $icon='';
        foreach ($info as $k=>$v){
            $filedata['pic'] = '/Uploads/Picture/' . $v['savepath'] . $v['savename'];
            $pic['md5'] = $v['md5'];
            $pic['sha1'] = $v['sha1'];
            $picture = M('picture')->where($pic)->find();
            if ($picture) {
                @unlink('.' . $filedata['pic']);
                //$result['pic'] = $picture['path'];
                $result['id'] = $picture['id'];
                $icon.=$picture['id'].',';
            } else {
                //存储图片
                $data['path'] = $filedata['pic'];
                //$data['cutpath'] = $cutpath;
                $data['url'] = '';
                $data['md5'] = $v['md5'];
                $data['sha1'] = $v['sha1'];
                $data['status'] = 1;
                $data['create_time'] = time();
                $pic_id = M('picture')->add($data);
                if($pic_id){
                    $icon.=$pic_id.',';
                }
            }
        }
        if($icon){
            $icon=substr($icon, 0, -1);
        }
        return $icon;
    }
}



